﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Runtime.Remoting.Messaging;
using HouodeFun.TrainCenter.Model;
using HouodeFun.TrainCenter.IDAL;

namespace HouodeFun.TrainCenter.DAL
{
    public class ObjectContextFactory : IObjectContextFactory
    {
        //执行的返回当前线程里面的唯一的上下文实例
        //pid => cache:  cache =>key=pid :objectcontext null new,
        public ObjectContext GetCurrentThreadInstance()
        {
            //线程内保证唯一实例
            
            //首先去线程数据槽里去取数据
            ObjectContext obj = CallContext.GetData(typeof(DataModelContainer).FullName) as ObjectContext;

            //检查线程内没有上下文，然后创建一个上下文并放入到线程数据槽里面去，然后再返回
            if (obj == null)
            {
                obj = new DataModelContainer();
                CallContext.SetData(typeof(DataModelContainer).FullName, obj);
            }

            return obj;
        }
    }
}
